<template>
  <div o-Container>
    <o-list v-bind:source="list.announcement"
            v-bind="{created:create,modified:modify}"/>
    <o-dialog v-bind:source="panel" v-bind="{opened:load,closed:load}">
      <template slot="content">
        <o-item v-bind:source="item.announcement" v-bind="{saved:save,backed:back}"></o-item>
      </template>
    </o-dialog>
  </div>
</template>


<script>
  export default {
    name: "mms_platform_announcement_list", /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */
    data() {
      return {
        octopus: this.$parent.octopus,
        list: {
          announcement: {
            data: {
              id: 131,
              cellData: [
                {name: "TypeID", width: 70, searchKey: "="},//类型id
                {name: "Title", width: 200, searchKey: "="},//标题
                {name: "Content", width: 500, searchKey: "LIKE"},//内容
                {name: "PushMsgID", width: 150},//推送信息id
                {name: "PushStatus", width: 100},//推送状态
                {name: "CreateUserID", width: 100},//创建者
                {name: "CreateTime", width: 130},//创建时间
              ],
            }
          },
        },
        item: {
          announcement: {
            data: {
              id: 131,
              value: "",
              cellData: [
                {name: "Title"},//标题
                {name: "Content"},//内容
                {name: "TypeID"},//类型
                {name: "CreateUserID"},//创建者
                {name: "CreateTime"},//创建时间
              ],
            }
          },
        },
        panel: {
          visible: false,
        },
      }
    },
    methods: {
      create: function () {
        this.item.announcement.data.value = null;
        this.item.announcement.control.load();
        this.panel.visible = true;
      },
      modify: function (row) {
        this.item.announcement.data.value = row.ID;
        this.item.announcement.control.load();
        this.panel.visible = true;
      },
      save: function (data) {
        let o = this;
        let appData = {
          data: {
            title: this.octopus.tools.parseCell(data.content, "Title").defaultValue,
            content: this.octopus.tools.parseCell(data.content, "Content").defaultValue,
          },
          templateID: "4"// 消息模版
        }

        this.octopus.push.base(appData, function (msg) {
          // alert(JSON.stringify(msg));
          if (msg.status.code == 200) {
            o.octopus.request.api.post("/Octopus/Engine/Item", {
                id: 131, // 采用巡检点编辑源，视图是不能更新的。
                type: "UPDATE",
                value: o.octopus.tools.parseCell(data.content, "ID").defaultValue,
                cellData: [
                  {name: "PushStatus", defaultValue: msg.content.responseCode},
                  {name: "PushMsgID", defaultValue: msg.content.msg_id},
                ],
              },
              // 处理回调事件，通过分析data.status.code的状态来实现。
              function (data) {
                // 刷新list
                o.list.announcement.control.load();
                o.back();
              });
            o.octopus.tips.success("保存成功并已经推送至APP端。");
          } else {
            o.octopus.tips.error(msg);
          }
        })
      },
      back: function () {
        this.panel.visible = false;
      },
    },
    props: ["oData"]
  }
</script>
